package com.bmt.pos.dao;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.bmt.core.BaseDao;
import com.bmt.pos.bean.ModuleInfo;
import com.bmt.pos.bean.User;
import com.bmt.utils.System_Utils;

@Repository("userModuleDao")
public class UserModuleDao extends BaseDao {

	public List<ModuleInfo> getmoduleInfo(User user, int upid) {
		String sql = "select *  from sys_moduleinfo where sys='" + System_Utils.SYS_NAME_POS
				+ "' and active=1 and upid =" + String.valueOf(upid)
				+ " and id in (select moduleid from sys_usermodule where userName='" + user.getUserName()
				+ "' ) order by indexVar";
		return getJdbcTemplate().query(sql, new BeanPropertyRowMapper<ModuleInfo>(ModuleInfo.class));
	}

	public List<ModuleInfo> getmoduleInfo(User user) {
		String sql = "select *  from sys_moduleinfo where sys='" + System_Utils.SYS_NAME_POS
				+ "' and active=1 and id in (select moduleid from sys_usermodule where userName='" + user.getUserName()
				+ "' ) order by indexVar";
		return getJdbcTemplate().query(sql, new BeanPropertyRowMapper<ModuleInfo>(ModuleInfo.class));
	}
	
	@Override
	public int getDataTotal() {
		return getJdbcTemplate().queryForInt(
				"select count(*) from sys_moduleinfo where sys='" + System_Utils.SYS_NAME_POS + "' and active=1");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ModuleInfo> getDataByPage(int pageNum, int pageSize) {
		return getJdbcTemplate().query(
				"select *  from sys_moduleinfo where sys='" + System_Utils.SYS_NAME_POS
						+ "' and active=1 order by upid,indexVar limit ?, ?",
				new BeanPropertyRowMapper<ModuleInfo>(ModuleInfo.class), pageNum, pageSize);
	}

}
